import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isNil from "lodash/isNil";
import _isNumber from "lodash/isNumber";
import _isFunction from "lodash/isFunction";
var _excluded = ["dataIndex", "title", "record", "contextConsumer", "children", "editable", "status", "render", "roleId", "editing", "options", "roleDatasource"];

var _dec, _dec2, _dec3, _dec4, _dec5, _class;

/**
 * EditableCell - 角色管理分配用户明细页面 - 行内编辑可编辑单元格组件
 * @date: 2018-10-26

 */
import React, { PureComponent } from 'react';
import { Form, Select } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from 'utils/intl';
import { getCurrentOrganizationId, isTenantRoleLevel, getCodeMeaning } from 'utils/utils';
import Lov from 'components/Lov';
import { VERSION_IS_OP } from 'utils/config'; // FormItem组件初始化

var FormItem = Form.Item;
var Option = Select.Option; // const organizationId = getCurrentOrganizationId();

/**
 * EditableCell - 送货单创建明细页面 - 行内编辑可编辑单元格组件
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {Object} form - 表单对象
 * @reactProps {object} contextProvider - Context.Provider
 * @return React.element
 */

var EditableCell = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), (_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(EditableCell, _PureComponent);

  var _super = _createSuper(EditableCell);

  function EditableCell() {
    _classCallCheck(this, EditableCell);

    return _super.apply(this, arguments);
  }

  _createClass(EditableCell, [{
    key: "onModelFieldIdChange",
    value:
    /**
     *
     *
     * @param {*} description
     * @param {*} getFieldDecorator
     * @memberof EditableCell
     */
    function onModelFieldIdChange(description, getFieldDecorator) {
      getFieldDecorator('fieldDescription', {
        initialValue: description
      });
    }
  }, {
    key: "onAssignLevelChange",
    value: function onAssignLevelChange(value) {
      var _this$props = this.props,
          _this$props$record = _this$props.record,
          record = _this$props$record === void 0 ? {} : _this$props$record,
          _this$props$assignRow = _this$props.assignRowData,
          assignRowData = _this$props$assignRow === void 0 ? function () {} : _this$props$assignRow,
          _this$props$options = _this$props.options,
          options = _this$props$options === void 0 ? [] : _this$props$options,
          dataIndex = _this$props.dataIndex,
          roleTenantId = _this$props.roleTenantId,
          roleTenantName = _this$props.roleTenantName;
      var newRecord = record;
      newRecord.assignLevel = value;
      newRecord["".concat(dataIndex, "Meaning")] = getCodeMeaning(value, options);
      Object.assign(newRecord, value === 'org' ? {
        assignLevelValue: null,
        assignLevelValueMeaning: null,
        sourceType: null
      } : {
        assignLevelValue: roleTenantId,
        // record.organizationId,
        assignLevelValueMeaning: roleTenantName // record.tenantName,

      });
      assignRowData(newRecord);
    }
  }, {
    key: "onIdChange",
    value: function onIdChange(selectedRow, form) {
      var _this$props2 = this.props,
          _this$props2$assignRo = _this$props2.assignRowData,
          assignRowData = _this$props2$assignRo === void 0 ? function () {} : _this$props2$assignRo,
          _this$props2$record = _this$props2.record,
          record = _this$props2$record === void 0 ? {} : _this$props2$record,
          _this$props2$roleData = _this$props2.roleDatasource,
          roleDatasource = _this$props2$roleData === void 0 ? {} : _this$props2$roleData;
      var _form$setFields = form.setFields,
          setFields = _form$setFields === void 0 ? function () {} : _form$setFields;
      var assignLevelDisabled = !_isNumber(record.organizationId) || roleDatasource.level === 'site' || !_isNil(roleDatasource.parentRoleAssignUnitId);
      var id = selectedRow.id,
          loginName = selectedRow.loginName,
          realName = selectedRow.realName,
          tenantName = selectedRow.tenantName;
      var newRecord = record;
      newRecord = _objectSpread(_objectSpread({}, newRecord), {}, {
        id: id,
        loginName: loginName,
        realName: realName,
        tenantName: tenantName,
        organizationId: VERSION_IS_OP && getCurrentOrganizationId() !== 0 ? getCurrentOrganizationId() : selectedRow.organizationId,
        assignLevelMeaning: null,
        assignLevelValueMeaning: assignLevelDisabled ? newRecord.assignLevelValueMeaning : null,
        assignLevelValue: assignLevelDisabled ? newRecord.assignLevelValue : VERSION_IS_OP && getCurrentOrganizationId() !== 0 ? selectedRow.organizationId : null
      });

      if (!assignLevelDisabled) {
        setFields({
          assignLevel: {
            value: undefined
          }
        });
      }

      assignRowData(newRecord);
    }
  }, {
    key: "onAssignLevelValueChange",
    value: function onAssignLevelValueChange(selectedRow) {
      var _this$props3 = this.props,
          _this$props3$assignRo = _this$props3.assignRowData,
          assignRowData = _this$props3$assignRo === void 0 ? function () {} : _this$props3$assignRo,
          _this$props3$record = _this$props3.record,
          record = _this$props3$record === void 0 ? {} : _this$props3$record;
      var newRecord = record;
      newRecord.assignLevelValue = selectedRow.unitId;
      newRecord.assignLevelValueMeaning = selectedRow.unitName;
      assignRowData(newRecord);
    }
    /**
     *
     *
     * @returns
     * @memberof EditableCell
     */

  }, {
    key: "getFormItem",
    value: function getFormItem() {
      var _this = this;

      var _this$props4 = this.props,
          dataIndex = _this$props4.dataIndex,
          title = _this$props4.title,
          _this$props4$record = _this$props4.record,
          record = _this$props4$record === void 0 ? {} : _this$props4$record,
          contextConsumer = _this$props4.contextConsumer,
          children = _this$props4.children,
          editable = _this$props4.editable,
          status = _this$props4.status,
          render = _this$props4.render,
          roleId = _this$props4.roleId,
          editing = _this$props4.editing,
          _this$props4$options = _this$props4.options,
          options = _this$props4$options === void 0 ? [] : _this$props4$options,
          roleDatasource = _this$props4.roleDatasource,
          restProps = _objectWithoutProperties(_this$props4, _excluded);

      var WrapperContextConsumer = contextConsumer;
      var tenantRoleLevel = isTenantRoleLevel();
      var defaultCellRender = _isFunction(render) ? render(record[dataIndex], record) : record[dataIndex];
      var defaultFormItems = {
        id: function id() {
          return /*#__PURE__*/React.createElement(WrapperContextConsumer, null, function () {
            var form = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

            var _ref = form || {},
                _ref$getFieldDecorato = _ref.getFieldDecorator,
                getFieldDecorator = _ref$getFieldDecorato === void 0 ? function (e) {
              return e;
            } : _ref$getFieldDecorato;

            return /*#__PURE__*/React.createElement(FormItem, {
              style: {
                margin: 0
              }
            }, getFieldDecorator(dataIndex, {
              initialValue: record[dataIndex],
              rules: [{
                required: true,
                message: intl.get("cpaas.common.validation.notNull", {
                  name: title
                }).d("".concat(title, "\u4E0D\u80FD\u4E3A\u7A7A"))
              }]
            })( /*#__PURE__*/React.createElement(Lov, {
              queryParams: {
                roleId: roleId
              },
              style: {
                width: 150
              },
              code: tenantRoleLevel ? 'CIAM.USER.ORG' : 'CIAM.USER',
              textValue: record.realName,
              onChange: function onChange(value, selectedRow) {
                return _this.onIdChange(selectedRow, form);
              }
            })));
          });
        },
        assignLevel: function assignLevel() {
          return /*#__PURE__*/React.createElement(WrapperContextConsumer, null, function () {
            var form = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

            var _ref2 = form || {},
                _ref2$getFieldDecorat = _ref2.getFieldDecorator,
                getFieldDecorator = _ref2$getFieldDecorat === void 0 ? function (e) {
              return e;
            } : _ref2$getFieldDecorat;

            return editing ? /*#__PURE__*/React.createElement(FormItem, {
              style: {
                margin: 0
              }
            }, getFieldDecorator(dataIndex, {
              initialValue: record[dataIndex],
              rules: [{
                required: true,
                message: intl.get("cpaas.common.validation.notNull", {
                  name: title
                }).d("".concat(title, "\u4E0D\u80FD\u4E3A\u7A7A"))
              }]
            })( /*#__PURE__*/React.createElement(Select // ref={node => {
            //   this.input = node;
            // }}
            , {
              style: {
                width: 120
              },
              onChange: _this.onAssignLevelChange.bind(_this),
              disabled: !_isNumber(record.organizationId) || roleDatasource.level === 'site' || !_isNil(roleDatasource.parentRoleAssignUnitId)
            }, options.map(function (o) {
              return /*#__PURE__*/React.createElement(Option, {
                key: o.value,
                value: o.value
              }, o.meaning);
            })))) : record.assignLevelMeaning;
          });
        },
        assignLevelValue: function assignLevelValue() {
          return /*#__PURE__*/React.createElement(WrapperContextConsumer, null, function () {
            var form = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

            var _ref3 = form || {},
                _ref3$getFieldDecorat = _ref3.getFieldDecorator,
                getFieldDecorator = _ref3$getFieldDecorat === void 0 ? function (e) {
              return e;
            } : _ref3$getFieldDecorat;

            return editing && record.assignLevel === 'org' && /*#__PURE__*/React.createElement(FormItem, {
              style: {
                margin: 0
              }
            }, getFieldDecorator(dataIndex, {
              initialValue: record[dataIndex],
              rules: [{
                required: true,
                message: intl.get("cpaas.common.validation.notNull", {
                  name: title
                }).d("".concat(title, "\u4E0D\u80FD\u4E3A\u7A7A"))
              }]
            })( /*#__PURE__*/React.createElement(Lov, {
              style: {
                width: 150
              },
              textValue: record.assignLevelValueMeaning,
              code: "CIAM.ASSIGN_LEVEL_VALUE_ORG",
              onChange: function onChange(value, selectedRow) {
                return _this.onAssignLevelValueChange(selectedRow, form);
              },
              queryParams: {
                roleId: roleId
              }
            })));
          });
        },
        action: function action() {
          return /*#__PURE__*/React.createElement(WrapperContextConsumer, null, function () {
            var form = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
            return render(record[dataIndex], record, form);
          });
        }
      };
      return /*#__PURE__*/React.createElement("td", restProps, dataIndex && defaultFormItems[dataIndex] && contextConsumer ? dataIndex === 'action' ? defaultFormItems[dataIndex]() : editing ? defaultFormItems[dataIndex]() : defaultCellRender : children || null);
    }
  }, {
    key: "render",
    value: function render() {
      return this.getFormItem();
    }
  }]);

  return EditableCell;
}(PureComponent), (_applyDecoratedDescriptor(_class.prototype, "onModelFieldIdChange", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "onModelFieldIdChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onAssignLevelChange", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "onAssignLevelChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onIdChange", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "onIdChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onAssignLevelValueChange", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "onAssignLevelValueChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getFormItem", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "getFormItem"), _class.prototype)), _class));
export { EditableCell as default };